Page({
  data: {
    keylist: [],
    hotkeylist:[],
    searchresultList: [],
    tablist:[
      {
        tabname:'实习',
        list:'internlist'
      },
      {
        tabname:'任务',
        list:'tasklist'
      },
      {
        tabname:'队伍',
        list:'teamlist'
      },
      {
        tabname:'动态',
        list:'dynamiclist'
      }
    ],
    currentkey: '',
    currenttabindex: 0,
    currentResultlist:[]
  },
  onLoad: function (options) {
    let keylist_str = wx.getStorageSync('SEARCH-KEYS').split(',').slice(0,4)
    this.setData({
      keylist: keylist_str,
      currentkey: options.key,
    })
    if (keylist_str.indexOf(this.data.currentkey) === -1) {
      this.data.keylist.push(this.data.currentkey)
      let keylist_str_updated = this.data.keylist.join(',')
      wx.setStorageSync('SEARCH-KEYS', keylist_str_updated)
    }
    keylist_str = wx.getStorageSync('SEARCH-KEYS')
    this.setData({
      keylist: keylist_str.split(','),
    })
    this.gethotsearchkeys()
    this.getresult()
  },
  onReady: function () {
    // this.getresult()
  },
  gethotsearchkeys: function(){
    wx.cloud.init({env: 'cloud1-5glocnmp62950062'})
    wx.cloud.callFunction({
      name:'getHotsearchkey',
      complete: res=>{
        this.setData({
          hotkeylist: res.result.hotsearchkeys.data,
        })
        console.log(res.result.hotsearchkeys.data)

        wx.hideLoading({
          success: (res) => {},
        })
      }
    })
  },
  getresult(e) {
    if(e){
      let searchkey = e.detail
      this.setData({
        currentkey:searchkey
      })
    }

    let keylist_str = wx.getStorageSync('SEARCH-KEYS')
    if (keylist_str.indexOf(this.data.currentkey) === -1) {
      this.data.keylist.reverse().push(this.data.currentkey)
      let keylist_str_updated = this.data.keylist.join(',')
      wx.setStorageSync('SEARCH-KEYS', keylist_str_updated)
    }
    keylist_str = wx.getStorageSync('SEARCH-KEYS')
    this.setData({
      keylist: keylist_str.split(',').reverse().slice(0,5),
    })
      
    wx.showLoading({
      title: '加载中，请稍后',
    })
    wx.cloud.init({env: 'cloud1-5glocnmp62950062'})
    wx.cloud.callFunction({
      name: 'getsearchresult',
      data:{
        key: this.data.currentkey
      },
      success: res=>{
        let searchresultList =  res.result
        searchresultList['dynamiclist'].list.map(item=>{
          item.like=item.love.length
          return item
        })
        this.setData({
          searchresultList:searchresultList,
          currentResultlist:searchresultList[`${this.data.tablist[this.data.currenttabindex].list}`]
        })
    this.gethotsearchkeys()

    wx.hideLoading({
      success: (res) => {},
    })
      },
      complete: res=>{

      }
    })
  },
  clickkeys: function(e){
    let key = e.currentTarget.dataset.key
    this.setData({
      currentkey: key,
    },
    ()=>{
      this.getresult()
    })
  },
  changetab: function(e){
    let searchresultList = this.data.searchresultList
    let index = e.currentTarget.dataset.index
    let currentlist = searchresultList[`${this.data.tablist[index].list}`]
    
    this.setData({
      currenttabindex: index,
      currentResultlist:currentlist
    })
  },
})
